"use strict";
cc._RF.push(module, '603a3rAJkNDyosE9QpKwHRN', 'loading');
// scripts/scenes/loading.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../engines/gameEngine");
var loadConfig_json_1 = require("./loading/loadConfig_json");
var gameConfig_1 = require("../engines/configs/gameConfig");
var logScreen_1 = require("../O1ModuleRes/frame/logScreen");
var MainRoot_1 = require("../Game/MainRoot");
var Enum_1 = require("../Game/Enum");
var loadingAni_1 = require("../O1ModuleRes/control/loadingAni");
var CsvDataManage_1 = require("../engines/managers/CsvDataManage");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var loading = /** @class */ (function (_super) {
    __extends(loading, _super);
    function loading() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.pmPreLoadScenes = true;
        _this.pmLoadBar = null;
        _this.pmSonLoadBar = null;
        _this.isShowSonBar = true;
        _this.sonLoadCount = 100;
        _this.pmLoadLabel = null;
        _this.loadingAni = null;
        _this.logo = null;
        _this.startBtn = null;
        _this.newBtn = null;
        _this.attentionBtn = null;
        _this.m_allPreloadResources = [];
        return _this;
    }
    loading_1 = loading;
    loading.prototype.start = function () {
    };
    loading.prototype.onLoad = function () {
        new CsvDataManage_1.default();
        if (loading_1.isLoaded) {
            var mainSceneName = gameEngine_1.managers().gameMgr.getMainSceneNameAndLoadBundle();
            if (typeof mainSceneName === 'string') {
                this.preLoadSceneOver(mainSceneName);
            }
            else {
                this.preLoadSceneOver(mainSceneName.sceneName);
            }
        }
        else {
            //
            this.pmLoadBar.progress = 0;
            this.pmLoadLabel.string = "\u6B63\u5728\u521D\u59CB\u5316";
            this.pmSonLoadBar.progress = 0;
            this.pmSonLoadBar.node.active = this.isShowSonBar;
            // this.startBtn.active = false;
            // this.newBtn.active =false;
            // this.attentionBtn.active=false;
            // this.logo.active = false;
            // this.loadingAni.node.active = true;
            //
            this.loadPackages();
        }
    };
    loading.prototype.onEnable = function () {
    };
    loading.prototype.loadPackages = function () {
        var _this = this;
        //
        this.loadSubpackages().then(function () {
            //
            var mainSceneName = gameEngine_1.managers().gameMgr.getMainSceneNameAndLoadBundle();
            if (typeof mainSceneName === 'string') {
                _this.preLoadSceneOver(mainSceneName);
            }
            else {
                _this.preLoadSceneOver(mainSceneName.sceneName);
            }
        });
    };
    loading.prototype.onAddLogServiceNode = function () {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve) {
                        if (gameConfig_1.default.LOG_SERVICE_OPEN &&
                            gameConfig_1.default.SCREEN_LOG_SWITCH) {
                            gameEngine_1.modules().uiControlModule.showFramePrefab('preload/frame/logScreen', gameEngine_1.default.Canvas().parent, false, function (node) {
                                gameEngine_1.services().logSrv.addScreenPrint(node.getComponent(logScreen_1.default));
                                resolve();
                            });
                        }
                        else {
                            resolve();
                        }
                    })];
            });
        });
    };
    loading.prototype.loadSubpackages = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: 
                    //
                    return [4 /*yield*/, gameEngine_1.default.loadAll()];
                    case 1:
                        //
                        _a.sent();
                        //
                        return [4 /*yield*/, this.loadAllPreloadResources()];
                    case 2:
                        //
                        _a.sent();
                        //
                        return [4 /*yield*/, this.onAddLogServiceNode()];
                    case 3:
                        //
                        _a.sent();
                        //
                        return [2 /*return*/, Promise.resolve()];
                }
            });
        });
    };
    //初始化要加载的数据
    loading.prototype.initPreloadResources = function () {
        // O1HD.getManagers().soundMgr.loadSoundBundle();
        //配置
        this.m_allPreloadResources = [];
        //框架包
        this.m_allPreloadResources.push({
            resourcesType: loadConfig_json_1.RESOURCES_TYPE.BUNDLE,
            resPathOrName: gameEngine_1.modules().uiControlModule.getFrameBundlePath(),
            loadText: '加载框架资源',
            onBundleLoaded: function (bundle) {
                gameEngine_1.modules().uiControlModule.setFrameBundle(bundle);
            },
        });
        //框架资源
        this.m_allPreloadResources.push({
            resourcesType: loadConfig_json_1.RESOURCES_TYPE.ASSETS,
            resPathOrName: gameEngine_1.modules().uiControlModule.getFrameBundlePreloadResPath(),
            loadText: '加载框架资源',
            bundle: function () {
                return gameEngine_1.modules().uiControlModule.getFrameBundle();
            },
        });
        //游戏包
        this.m_allPreloadResources.push({
            resourcesType: loadConfig_json_1.RESOURCES_TYPE.BUNDLE,
            resPathOrName: gameEngine_1.modules().uiControlModule.getGameBundlePath(),
            loadText: '加载游戏公共资源',
            onBundleLoaded: function (bundle) {
                gameEngine_1.modules().uiControlModule.setGameBundle(bundle);
            },
        });
        //游戏资源
        this.m_allPreloadResources.push({
            resourcesType: loadConfig_json_1.RESOURCES_TYPE.ASSETS,
            resPathOrName: gameEngine_1.modules().uiControlModule.getGameBundlePreloadResPath(),
            loadText: '加载游戏资源',
            bundle: function () {
                return gameEngine_1.modules().uiControlModule.getGameBundle();
            },
        });
        //游戏主音效
        this.m_allPreloadResources.push({
            resourcesType: loadConfig_json_1.RESOURCES_TYPE.BUNDLE,
            resPathOrName: "mainSound",
            loadText: "加载游戏音效",
            onBundleLoaded: function (bundle) {
                gameEngine_1.managers().soundMgr.addOneSoundBundle("mainSound", bundle);
            },
        });
        //hall分包
        this.m_allPreloadResources.push({
            resourcesType: loadConfig_json_1.RESOURCES_TYPE.BUNDLE,
            resPathOrName: 'hall',
            loadText: '加载总场景资源',
        });
        //主场景资源
        var mainSceneBundle = gameEngine_1.managers().gameMgr.getMainSceneNameAndLoadBundle();
        if (null == mainSceneBundle) {
            console.error('#69 主场景配置错误');
        }
        //主场景 为 hall
        if (mainSceneBundle === 'hall') {
            this.m_allPreloadResources.push({
                resourcesType: loadConfig_json_1.RESOURCES_TYPE.SCENE,
                resPathOrName: mainSceneBundle,
                loadText: '加载主场景'
            });
            //否则就调用其他子包
        }
        else {
            var bundleInfo_1 = mainSceneBundle;
            //先加载子包
            this.m_allPreloadResources.push({
                resourcesType: loadConfig_json_1.RESOURCES_TYPE.BUNDLE,
                resPathOrName: bundleInfo_1.name,
                loadText: '加载场景资源',
                onBundleLoaded: function (bundle) {
                    bundleInfo_1.bundle = bundle;
                    gameEngine_1.managers().gameMgr.setMainModeLoaded(bundleInfo_1);
                }
            });
            //
            this.m_allPreloadResources.push({
                resourcesType: loadConfig_json_1.RESOURCES_TYPE.SCENE,
                resPathOrName: bundleInfo_1.sceneName,
                loadText: '加载主场景资源',
            });
        }
    };
    loading.prototype.loadAllPreloadResources = function () {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        //
                        this.initPreloadResources();
                        //
                        return [4 /*yield*/, this.startPreloadResources()];
                    case 1:
                        //
                        _a.sent();
                        return [4 /*yield*/, gameEngine_1.default.getManagers().csvDataManager.initCsvList()];
                    case 2:
                        _a.sent();
                        //
                        return [2 /*return*/, Promise.resolve()];
                }
            });
        });
    };
    //开始加载数据
    loading.prototype.startPreloadResources = function () {
        return __awaiter(this, void 0, Promise, function () {
            var length, i, time1, currLoad, startProgress, time2;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        length = this.m_allPreloadResources.length;
                        i = 0;
                        _a.label = 1;
                    case 1:
                        if (!(i < length)) return [3 /*break*/, 4];
                        time1 = Date.now();
                        currLoad = this.m_allPreloadResources[i];
                        startProgress = i / length;
                        this.setLoadProgress(startProgress, currLoad.loadText);
                        //
                        return [4 /*yield*/, this.preloadOneResources(currLoad, startProgress)];
                    case 2:
                        //
                        _a.sent();
                        time2 = Date.now();
                        console.log("\u52A0\u8F7D" + currLoad.loadText + "\u8017\u65F6" + (time2 - time1));
                        _a.label = 3;
                    case 3:
                        i++;
                        return [3 /*break*/, 1];
                    case 4: return [2 /*return*/, Promise.resolve()];
                }
            });
        });
    };
    //加载一个
    loading.prototype.preloadOneResources = function (currLoad, startProgress) {
        return __awaiter(this, void 0, Promise, function () {
            var _this = this;
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve) {
                        var resourcesType = currLoad.resourcesType, resPathOrName = currLoad.resPathOrName, bundle = currLoad.bundle, onBundleLoaded = currLoad.onBundleLoaded, onComplete = currLoad.onComplete;
                        //资源类型
                        switch (resourcesType) {
                            //分包
                            case loadConfig_json_1.RESOURCES_TYPE.BUNDLE: {
                                cc.assetManager.loadBundle(resPathOrName, function (err, bundle) {
                                    onBundleLoaded && onBundleLoaded(bundle);
                                    resolve();
                                });
                                break;
                            }
                            case loadConfig_json_1.RESOURCES_TYPE.ASSETS: {
                                bundle().loadDir(resPathOrName, function (finish, total) {
                                    _this.setSonLoadProgress(finish / total, startProgress);
                                }, function (error, assets) {
                                    onComplete && onComplete(error, assets);
                                    resolve();
                                });
                                break;
                            }
                            case loadConfig_json_1.RESOURCES_TYPE.SCENE: {
                                cc.director.preloadScene(resPathOrName, function (completedCount, totalCount) {
                                    _this.setSonLoadProgress(completedCount / totalCount, startProgress);
                                }, function (error) {
                                    resolve();
                                });
                                break;
                            }
                        }
                    })];
            });
        });
    };
    loading.prototype.preLoadSceneOver = function (mainSceneName) {
        gameEngine_1.default.loadScene(mainSceneName, function () {
            gameEngine_1.default.getManagers().gameMgr.onGameRun();
            loading_1.isLoaded = true;
            // this.pmLoadBar.node.active = false;
            // this.logo.active = true;
            // this.startBtn.active = true;
            // this.newBtn.active =true;
            // this.attentionBtn.active=true;
            // this.loadingAni.node.active = false;
            gameEngine_1.default.getServices() && gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance && gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance() && gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance().WidgetfullScreen && gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance().WidgetfullScreen(); //屏幕适配
        });
        // })
    };
    loading.prototype.setSonLoadProgress = function (progress, startProgress) {
        this.pmSonLoadBar.progress = progress;
        var length = this.m_allPreloadResources.length;
        this.setLoadProgress(startProgress + progress / length);
    };
    loading.prototype.setLoadProgress = function (progress, text) {
        // console.log(progress,"进度>>>>>>>>>")
        this.pmLoadBar.progress = progress;
        this.loadingAni.setProgress(progress);
        if (text) {
            this.pmLoadLabel.string = text + "(" + (progress * 99.99).toFixed(2) + "%)";
        }
    };
    loading.prototype.onBtnStart = function (e, custom) {
        if (loading_1.isLoaded) {
            gameEngine_1.managers().soundMgr.playSound("mainSound/anNiu");
            gameEngine_1.managers().soundMgr.playVibrate();
            var mode = Number(custom);
            var levelMenuPrams = {
                mode: mode
            };
            MainRoot_1.default.UISystem.OpenPage(Enum_1.PAGE.LEVELMENU, levelMenuPrams);
        }
        else {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("游戏正在加载中...");
        }
    };
    var loading_1;
    loading.isLoaded = false;
    __decorate([
        property({ displayName: '预加载场景', tooltip: '是否预先加载大厅和游戏场景' })
    ], loading.prototype, "pmPreLoadScenes", void 0);
    __decorate([
        property({ type: cc.ProgressBar, displayName: '进度条' })
    ], loading.prototype, "pmLoadBar", void 0);
    __decorate([
        property({ type: cc.ProgressBar, displayName: '子进度条' })
    ], loading.prototype, "pmSonLoadBar", void 0);
    __decorate([
        property({ displayName: '是否显示子进度条' })
    ], loading.prototype, "isShowSonBar", void 0);
    __decorate([
        property({ displayName: '子进度条加载条数' })
    ], loading.prototype, "sonLoadCount", void 0);
    __decorate([
        property({ type: cc.Label, displayName: '进度文字' })
    ], loading.prototype, "pmLoadLabel", void 0);
    __decorate([
        property({ type: loadingAni_1.default, displayName: "加载中动画" })
    ], loading.prototype, "loadingAni", void 0);
    __decorate([
        property({ type: cc.Node, displayName: "" })
    ], loading.prototype, "logo", void 0);
    __decorate([
        property({ type: cc.Node, displayName: "" })
    ], loading.prototype, "startBtn", void 0);
    __decorate([
        property({ type: cc.Node, displayName: "" })
    ], loading.prototype, "newBtn", void 0);
    __decorate([
        property({ type: cc.Node, displayName: "关注抖音号" })
    ], loading.prototype, "attentionBtn", void 0);
    loading = loading_1 = __decorate([
        ccclass
    ], loading);
    return loading;
}(cc.Component));
exports.default = loading;

cc._RF.pop();